//
//  LGSelectButtonItemView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/25.
//

#import "LGSelectButtonItemView.h"

@interface LGSelectButtonItemView()

/**   */
@property (nonatomic , strong)NSMutableArray *buttonArray;

@end

@implementation LGSelectButtonItemView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)setResultArray:(NSArray *)resultArray{
    _resultArray = resultArray;
}

-(void)setResultString:(NSString *)resultString{
    _resultString = resultString;
}


-(void)setItemArray:(NSArray *)itemArray{
    _itemArray = itemArray;
    CGFloat totalHeight = 0;
    for (NSInteger i=0; i<self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        [button removeFromSuperview];
    }
    [self.buttonArray removeAllObjects];
    CGFloat top = 0;
    CGFloat left = 0;
    CGFloat height = 34;
    CGFloat margin_H = viewPix(12);
    CGFloat margin_V = viewPix(16);
    CGFloat maxWidth = self.bounds.size.width;
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSString *title = LGNSString(itemArray[i][@"title"]);
        CGFloat textWidth = [title textWidthWithFont:LGFontWeight(15, UIFontWeightMedium) maxHeight:MAXFLOAT]+40;
        top = ((left+textWidth)>=maxWidth)?(top+height+margin_V):top;
        left = ((left+textWidth)>=maxWidth)?0:left;
        UIButton *button = [UIButton buttonWithTitle:title titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        button.frame = CGRectMake(left, top, textWidth, height);
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        button.selected = ([self.resultArray containsObject:title] || [self.resultString isEqualToString:LGNSString(itemArray[i][@"code"])])?YES:NO;
        button.cornerRidus = 17;
        button.tag = 1000+i;
        [button addTarget:self action:@selector(itemButtonTouchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [self.buttonArray addObject:button];
        left += textWidth+margin_H;
        if(i == itemArray.count-1){
            totalHeight = top+height;
        }
    }
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(selectItemViewHeight:tag:)]){
        [self.delegate selectItemViewHeight:totalHeight tag:self.tag];
    }
}




-(void)itemButtonTouchAction:(UIButton *)sender{
    if([self.type isEqualToString:@"radio"]){
        //单选
        for (UIButton *button in self.buttonArray) {
            button.selected = NO;
        }
        sender.selected = YES;
        NSDictionary *tempDic = self.itemArray[sender.tag-1000];
        if(self.delegate && [self.delegate respondsToSelector:@selector(selectItemResultSting:tag:)]){
            [self.delegate selectItemResultSting:LGNSString(tempDic[@"code"]) tag:self.tag];
        }
    }else{
        //多选
        sender.selected = !sender.selected;
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSInteger i=0; i<self.buttonArray.count; i++) {
            UIButton *button = self.buttonArray[i];
            if(button.selected == YES){
                NSDictionary *tempDic = self.itemArray[button.tag-1000];
                [tempArray addObject:LGNSString(tempDic[@"code"])];
            }
        }
        if(self.delegate && [self.delegate respondsToSelector:@selector(selectItemResultArray:tag:)]){
            [self.delegate selectItemResultArray:tempArray tag:self.tag];
        }
    }
}


-(NSMutableArray *)buttonArray{
    if(!_buttonArray){
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

@end
